package com.example.student.fragment;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.location.Location;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.DatePicker;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.example.student.MainActivity;
import com.example.student.R;
import com.example.student.activity.AddTaskActivity;
import com.example.student.adapter.DoTaskAdapter;
import com.example.student.bean.TaskBean;
import com.example.student.myinterface.PopInterface;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
//import com.scwang.smartrefresh.layout.api.RefreshLayout;
//import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
//import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

/**
 * 做作业
 */
public class DoTaskFragment extends Fragment implements View.OnClickListener {

    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    private View mView;
    private RefreshLayout mPtrLayout;
    private RecyclerView mRecyclerView;
    private DoTaskAdapter doTaskAdapter;
    private String TAG = "DoTaskFragment";
    private int mListPage = 1;
    private TextView tv_time,tv_add_task;
    private Calendar calendar = null;
    private int year, month, day;

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;

    private MainActivity mainActivity ;



    public DoTaskFragment(Activity activity) {
        this.mainActivity = (MainActivity) activity;
    }
    public DoTaskFragment() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment DoTaskFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static DoTaskFragment newInstance(String param1, String param2) {
        DoTaskFragment fragment = new DoTaskFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        mView = inflater.inflate(R.layout.fragment_do_task, container, false);
        return mView;
    }

    @Override
    public void onViewCreated(@NonNull @NotNull View view, @Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView();
        initData();
    }

    private void initView() {

        mPtrLayout = mView.findViewById(R.id.fragment_ptr);
        mRecyclerView = mView.findViewById(R.id.recycle_do);
        tv_time = mView.findViewById(R.id.tv_time);
        tv_add_task = mView.findViewById(R.id.tv_add_task);
        tv_time.setOnClickListener(this);
        tv_add_task.setOnClickListener(this);

        calendar = Calendar.getInstance();//获取当前时间的日历，不然默认1900年

        year = calendar.get(Calendar.YEAR);//今年
        month = calendar.get(Calendar.MONTH);//今月
        day = calendar.get(Calendar.DAY_OF_MONTH);//今日
        show(year, month, day);

    }


    private void initData() {

        mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        ArrayList mList = new ArrayList<TaskBean>();

        TaskBean taskBean = new TaskBean();
        taskBean.setSubject("数学");

        taskBean.setPractice("数学册");
        taskBean.setPractice_correct("未批改");
        taskBean.setPractice_finish("写完了222");
        taskBean.setPractice_nodo("eeizuo");

        TaskBean taskBean1 = new TaskBean();
        taskBean1.setSubject("英语");
        taskBean1.setPractice("英语练习册");
        taskBean1.setPractice_correct("已经批改完成");
        taskBean1.setPractice_finish("写完了");

        taskBean1.setArithmetic("口算");
        taskBean1.setArithmetic_correct("未批改");
        taskBean1.setArithmetic_finish("已完成");

        mList.add(taskBean);
        mList.add(taskBean1);
        mList.add(taskBean);
        mList.add(taskBean1);

        doTaskAdapter = new DoTaskAdapter(getActivity(),mainActivity,mList);
        mRecyclerView.setAdapter(doTaskAdapter);

    }

    private void pullList() {
        mPtrLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                Log.e(TAG, "//去吧onRefresh。。 。。。");
                mListPage = 1;
//                getNewsData();
            }
        });
        mPtrLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(RefreshLayout refreshLayout) {

            }
        });
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_time:
                setTime();
                break;
            case R.id.tv_add_task:
                Intent in = new Intent(getActivity(),AddTaskActivity.class);
                startActivity(in);

                break;
        }

    }

    private void setTime() {

        DatePickerDialog datePickerDialog = new DatePickerDialog(getActivity(), new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
                calendar.set(Calendar.YEAR, year);
                calendar.set(Calendar.MONTH, month);
                calendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
                show(year, month, dayOfMonth);
            }

        }, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH));
        datePickerDialog.show();
    }

    private void show(int year, int month, int day) {
        String str = year + "年" + (month + 1) + "月" + day + "日";
        Log.e("时间str", str);
        tv_time.setText(str);
    }
}
